<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="../../js/jquery-3.3.1.min.js" charset="utf-8"></script>
    <link rel="stylesheet" href="/layui-v2.4.5/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">添加优惠券</button>
        <%--<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>--%>
        <%--<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>--%>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="/layui-v2.4.5/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:getPath()+'/shop/allcoupon'
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'name', title:'优惠券名称', width:120, edit: 'text'}
                ,{field:'mixMoney', title:'最低消费价格', width:150, edit: 'text', templet: function(res){
                        return '<em>'+ res.mixMoney +'</em>'
                    }
                    }
                ,{field:'money', title:'价值金额', width:80, edit: 'text', sort: true}
                ,{field:'price', title:'售卖价格', width:100}
                ,{field:'startTime', title:'有效期开始时间',width:160}
                ,{field:'endTime', title:'有效期结束时间', width:160, sort: true}
                ,{field:'createTime', title:'创建时间', width:180}
                ,{field:'giveType', title:'优惠卷类型', width:130,  templet: function(res){
                    if(res.giveType=="0"){
                        return '<em>'+ "购买赠送" +'</em>'
                    }else if(res.giveType=="1") {
                        return '<em>' + "转发给朋友赠与" + '</em>'
                    }
                    }

                }
                ,{field:'state', title:'状态', width:120,templet: function(res){
                        if(res.state=="0"){
                            return '<em>'+ "可用" +'</em>'
                        }else if(res.state=="1") {
                            return '<em>' + "转发给朋友赠与" + '</em>'
                        }
                    }
                }
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    layer.open({
                        type: 2,
                        title:"添加商户",
                        shadeClose:true,
                        offset: '30px',
                        area: ['75%', '75%'],
                        content: '/shop/enterAddcoupon',
                        end:function () {
                            window.location.reload();
                        }
                    });
                case 'getCheckLength':
                    var data = checkStatus.data;
                    // layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            console.log(obj)
            if(obj.event === 'del'){
                var dataStr=JSON.stringify(data);
                console.log(dataStr)
                $.ajax({
                    url:'/shop/delyouhuijuan?id='+data.id,
                    type:'POST', //GET
                    async:true,    //或false,是否异步
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    beforeSend:function(xhr){
                        /*console.log(xhr)
                         console.log('发送前')*/
                    },
                    success:function(data){
                        console.log(data);
                        if (data.code=='000000'){
                            window.location.reload();
                        }else {
                            layer.msg(data.msg,{time:1000,icon: 5});
                        }
                        //console.log(data)
                    },
                    error:function(xhr,textStatus){
                        console.log('错误')
                        console.log(xhr)
                        console.log(textStatus)
                    },
                    complete:function(){
                        //console.log('结束')
                    }
                });




            } else if(obj.event === 'edit'){
                var dataStr=JSON.stringify(data);
                console.log(dataStr)
                layer.open({
                    type: 2,
                    title:"添加商户",
                    shadeClose:true,
                    offset: '30px',
                    area: ['75%', '75%'],
                    content: '/shop/enterAddcoupon?id='+data.id,
                    end:function () {
                        window.location.reload();
                    }
                });
            }
        });
    });


    /**
     * 项目路径
     */

    function getPath(){
        //获取当前网址，如： http://localhost:80/ybzx/index.jsp
        var curPath=window.document.location.href;
        //获取主机地址之后的目录，如： ybzx/index.jsp
        var pathName=window.document.location.pathname;
        var pos=curPath.indexOf(pathName);
        //获取主机地址，如： http://localhost:80
        var localhostPaht=curPath.substring(0,pos);
        //获取带"/"的项目名，如：/ybzx
        var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
        //return(localhostPaht+projectName);
        var path2= localhostPaht+projectName;
        return localhostPaht;
    }
</script>
</body>
</html>